package org.myh.excel.uitl.strategy;

/**
 * ClassName: DefaultTypeConvert
 * Description: 默认实现
 * date: 2024/3/25 15:12
 *
 * @author Yu Han
 * @since JDK 1.8
 */
public class DefaultTypeConvert implements TypeConvert<Object> {

    public static final String STRING = "java.lang.String";
    public static final String INTEGER = "java.lang.Integer";
    public static final String SHORT = "java.lang.Short";
    public static final String LONG = "java.lang.Long";
    public static final String DOUBLE = "java.lang.Double";
    public static final String FLOAT = "java.lang.Float";
    public static final String CHAR = "java.lang.Character";
    public static final String BOOLEAN = "java.lang.Boolean";
    public static final String INT = "int";
    public static final String G_SHORT = "short";
    public static final String G_LONG = "long";
    public static final String G_DOUBLE = "double";
    public static final String G_FLOAT = "float";
    public static final String G_CHAR = "char";
    public static final String G_BOOLEAN = "boolean";

    /**
     * 默认不采用策略模式，若后续有不同的类型需要，可以拓展TypeConvert实现策略
     * 这里只是做字符串判断
     *
     * @param val
     * @return
     */
    @Override
    public Object getConvertValue(String type, String val) {
        if (val == null ||"".equals(val.trim())) {
            return null;
        }

        if (type.equals(INTEGER) || type.equals(INT)) {
            return Integer.valueOf(val);
        } else if (type.equals(SHORT) || type.equals(G_SHORT)) {
            return Short.valueOf(val);
        } else if (type.equals(LONG) || type.equals(G_LONG)) {
            return Long.valueOf(val);
        } else if (type.equals(DOUBLE) || type.equals(G_DOUBLE)) {
            return Double.valueOf(val);
        } else if (type.equals(FLOAT) || type.equals(G_FLOAT)) {
            return Float.valueOf(val);
        } else if (type.equals(CHAR) || type.equals(G_CHAR)) {
            return val.toCharArray()[0];
        } else if (type.equals(BOOLEAN) || type.equals(G_BOOLEAN)) {
            return Boolean.valueOf(val);
        }
        return val;
    }

}
